/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef OLA_SECURITY_H
#define OLA_SECURITY_H

#include "ola_def.h"
#include "ola_sdk_api.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {
    OLA_AES_128_GCM_KEY_LEN = 16,
    OLA_AES_192_GCM_KEY_LEN = 24,
    OLA_AES_256_GCM_KEY_LEN = 32,
} OlaAesGcmKeyLen;

#define OLA_AES_GCM_TAG_MAX_LEN 16
#define OLA_AES_GCM_TAG_MIN_LEN 4

typedef struct {
    /* 加解密密钥，长度为16、24或32字节 */
    OlaData key;
    /* 初始向量 */
    OlaData iv;
    /* 附加值，可以为空 */
    OlaData add;
    /* tag值，加密时为空 */
    OlaData tag;
    /* 待加解密数据 */
    OlaData inData;
} AesGcmParam;

/*
 * 功能: AES GCM加密
 * 参数: input，加密参数，详见AesGcmParam定义
 *       output，加密结果
 *       tag，加密生成的tag值，size值在[4,16]字节
 * 返回: OLA_OK，加密成功
 *       非OLA_OK，加密失败
 */
int OlaAesGcmEncript(const AesGcmParam *input, OlaBuffer *output, OlaBuffer *tag);

/*
 * 功能: AES GCM解密
 * 参数: input，解密参数，详见AesGcmParam定义
 *       output，解密结果
 * 返回: OLA_OK，解密成功
 *       非OLA_OK，解密失败
 */
int OlaAesGcmDecript(const AesGcmParam *input, OlaBuffer *output);

int OlaEncodeBase64Data(const OlaData *inData, OlaData *outData);

int OlaDecodeBase64Data(const OlaData *inData, OlaData *outData);

int OlaComputeMdHmac(const OlaData *key, const OlaData *inData, unsigned char outBuf[OLA_MD_HMAC_LEN]);

int OlaSha256(const unsigned char *input, unsigned int inputLen, unsigned char output[OLA_SHA256_HEX_LEN]);

#ifdef __cplusplus
}
#endif

#endif /* OLA_SECURITY_H */